package com.bjypc.youth.web.json;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.bjypc.youth.entity.Stock;
import com.opensymphony.xwork2.ActionSupport;

public class StockAction extends ActionSupport {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 3257392510918950171L;
	private List<Stock> stockLists = new ArrayList<Stock>();

	@Override
	public String execute() throws Exception {

		Random random = new Random();
		for (int i = 0; i < 20; i++) {
			Stock stock = new Stock();
			stock.setCompany("abc" + random.nextInt(30));
			float change = random.nextFloat();
			
			stock.setChange(round(change, 3));
			stock.setLastChange(new Date());
			stock.setPrice(round(random.nextFloat(),3));
			double pctChange = round(random.nextDouble(),3);
			if (i % 3 == 0)
				stock.setPctChange(pctChange * -1);
			else
				stock.setPctChange(pctChange);
			this.stockLists.add(stock);
		}

		return SUCCESS;
	}

	private static double round(double v, int scale) {
		String temp = "#,##0.";
		for (int i = 0; i < scale; i++) {
			temp += "0";
		}
		return Double.valueOf(new java.text.DecimalFormat(temp).format(v));
	}

	public List<Stock> getStockLists() {
		return stockLists;
	}

	public void setStockLists(List<Stock> stockLists) {
		this.stockLists = stockLists;
	}

}
